using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MSRI.HelperClasses;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Xml;
using System.IO;
using Instedd.Labs.Geo.Library.Kml;
using Instedd.Labs.Geo.Library.Kml.Styles;
using Instedd.Labs.Geo.Library.Sms;

namespace Instedd.Labs.Geo.Library.Tests.Managers
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class KmlManagerFixture
    {
        public KmlManagerFixture()
        {}

        [TestMethod]
        public void EmptyMessageListCreatesAnEmptyKml()
        {
            KmlManager manager = new KmlManager();

            KmlFormat kml = manager.CreateKml(new SmsExtension[] { });

            Assert.IsNotNull(kml);
            Assert.AreEqual(0, kml.Folders.Count);
        }

        [TestMethod]
        public void SmsExtensionToPlacemarkMapsNameDescriptionAndPoint()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland", "Tom", "444")
            };
            DateTime timestamp = new DateTime(2000, 1, 7, 18, 30, 0);
            messages[0].Sms.Time = timestamp;

            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(1, kml.Folders.Count);
            Assert.AreEqual(0, kml.Folders[0].Paths.Count);
            Assert.AreEqual(1, kml.Folders[0].Placemarks.Count);
            Assert.AreEqual("Tom", kml.Folders[0].Placemarks[0].Name);
            Assert.AreEqual("<i>Last message at 18:30 (01-07-2000)</i><br/><b>Tom:</b> I'm at Kirkland<br/>", kml.Folders[0].Placemarks[0].Description);
            Assert.AreEqual(new LatLong(47.68166999999999 , - 122.2075), kml.Folders[0].Placemarks[0].Location);
        }

        [TestMethod]
        public void TwoMessagesWithDifferentLocationAndSameSenderNameGeneratesAPathBetweenLocations()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444")
            };

            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(1, kml.Folders[0].Paths.Count);
            Assert.AreEqual(2, kml.Folders[0].Paths[0].Coordinates.Count);
            Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Paths[0].Coordinates[0]);
            Assert.AreEqual(new LatLong(47.67861000000001, - 122.13099), kml.Folders[0].Paths[0].Coordinates[1]);
        }

        [TestMethod]
        public void TwoDifferentMessagesFromTheSameSenderNameAndLocationGeneratesOneSinglePlacemarkWithBothMessages()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland.", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "And I'm going to Redmond.", "Tom", "444")
            };

            DateTime timestamp = new DateTime(2000, 1, 7, 12, 30, 0);
            messages[1].Sms.Time = timestamp;


            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(1, kml.Folders[0].Placemarks.Count);
            Assert.AreEqual("Tom", kml.Folders[0].Placemarks[0].Name);
            Assert.AreEqual("<i>Last message at 12:30 (01-07-2000)</i><br/><b>Tom:</b> I'm at Kirkland.<br/><b>Tom:</b> And I'm going to Redmond.<br/>", kml.Folders[0].Placemarks[0].Description);
            Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Placemarks[0].Location);
        }



        [TestMethod]
        public void TwoDifferentMessagesFromTheDifferentSenderNameGeneratesTowFolders()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland (Tom).", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075), "I'm at Kirkland (John).", "John", "444")
            };

            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(2, kml.Folders.Count);
            Assert.AreEqual("Tom", kml.Folders[0].Name);
            Assert.AreEqual("John", kml.Folders[1].Name);
            Assert.AreEqual("Tom", kml.Folders[0].Placemarks[0].Name);
            Assert.AreEqual("John", kml.Folders[1].Placemarks[0].Name);
            Assert.AreEqual(new LatLong(47.68166999999999, -122.2075), kml.Folders[0].Placemarks[0].Location);
            Assert.AreEqual(new LatLong(47.68166999999999, - 122.2075), kml.Folders[1].Placemarks[0].Location);
        }

        [TestMethod]
        public void MessageFromDifferentLocationAtDifferentTimesCreatesPlacemarksUsingDifferentStylesBasedOnTheArrivedTime()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075),  "I'm at Kirkland", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.10099), "I'm at Redmond", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.11099), "I'm at Redmond", "Tom", "444")
            };

            messages[0].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 60, 0));
            messages[1].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 45, 0));
            messages[2].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 25, 0));
            messages[3].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 10, 0));


            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[0].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory3Style, kml.Folders[0].Placemarks[1].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory2Style, kml.Folders[0].Placemarks[2].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory1Style, kml.Folders[0].Placemarks[3].StyleUrl);
        }

        [TestMethod]
        public void MessageFromDifferentLocationAtDifferentTimesCreatesLatestPlacemarkInThreadWith100PercentOpaqueNotMatterTheTimeItArrived()
        {
            KmlManager manager = new KmlManager();

            SmsExtension[] messages = new SmsExtension[]{ 
                new SmsExtension(new SmsClass(), new LatLong(47.68166999999999, -122.2075),  "I'm at Kirkland", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.13099), "I'm at Redmond", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.10099), "I'm at Redmond", "Tom", "444"),
                new SmsExtension(new SmsClass(), new LatLong(47.67861000000001, -122.11099), "I'm at Redmond", "Tom", "444")
            };

            messages[0].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 100, 0));
            messages[1].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 80, 0));
            messages[2].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 70, 0));
            messages[3].Sms.Time = DateTime.Now.Subtract(new TimeSpan(0, 50, 0));


            KmlFormat kml = manager.CreateKml(messages);

            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[0].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[1].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory4Style, kml.Folders[0].Placemarks[2].StyleUrl);
            Assert.AreEqual(StylesUtil.MessageMinutesAgoCategory1Style, kml.Folders[0].Placemarks[3].StyleUrl);
        }
    }
}
